package com.kaifamiao.loginmodel.controller;

import com.kaifamiao.loginmodel.entity.UserInfo;
import com.kaifamiao.loginmodel.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class Login {
    @Autowired
    private UserInfoService userService;
    @GetMapping ("/show")
    public List<UserInfo> showUser() {
        return userService.getAllUser();
    }
    @GetMapping ("/login")
    public Map<String, Object>  login(String username, String password) {
        UserInfo userInfo = userService.getUserByName(username);
        Map<String, Object> response = new HashMap<>();

        if (userInfo == null) {
            System.out.println("null");
            response.put("code", "200");
            response.put("status", "error");
            response.put("message", "用户不存在");
            System.out.println(response);
        }else{
            if (userInfo.getPassword().equals(password)){
                response.put("code", "200");
                response.put("status", "successfully");
                response.put("message", "登录成功");
                Map<String, Object> data = new HashMap<>();
                data.put("username", userInfo.getUsername());
                data.put("id", userInfo.getId());
                // 将模拟数据添加到响应中
                response.put("data", data);
            }
            else{
                response.put("code", "200");
                response.put("status", "success");
                response.put("message", "密码不正确");
            }
        }



        return response;

    }

}
